package com.thingsgrid.device.controller;

import com.thingsgrid.common.constant.ResponseCodeConstant;
import com.thingsgrid.device.base.AbstractDevice;
import com.thingsgrid.device.dto.DeviceDTO;
import com.thingsgrid.device.entity.Device;
import com.thingsgrid.device.entity.Product;
import com.thingsgrid.device.entity.Shadow;
import com.thingsgrid.device.service.IDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springblade.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@RestController
//@AllArgsConstructor
@RequestMapping("/device")
@Api(value = "", tags = "接口")
public class DeviceController extends AbstractDevice{
	@Autowired
	private IDeviceService deviceService;
	//@Autowired
	//private IDictClient client2;
	/**
	 * 新增
	 */
	@PostMapping("/submitPublish")
	@ApiOperation(value = "新增", notes = "传入device", position = 1)
	public void save(@Valid @RequestBody String productType) {
//		deviceService.saveDevicePublish(device);
	}
	/**
	 * 新增
	 */
	@PostMapping("/addDevice")
	@ApiOperation(value = "新增", notes = "传入device", position = 1)
	public R save(@Valid Device device) {
		if(deviceService.saveDevice(device)==null){
			return R.fail(ResponseCodeConstant.DATAACCESS_ERROR.getCode(),"新增失败!");
		}else{
			return R.success(ResponseCodeConstant.RESPONSE_SUCCESS.getCode().toString());
		}
	}
	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperation(value = "逻辑删除", notes = "传入ids", position = 2)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return deviceService.removeDevice(ids)==1? R.success("删除成功"): R.fail(ResponseCodeConstant.DATAACCESS_ERROR.getCode(),"删除失败");
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "传入product", position = 3)
	public void update(@Valid @RequestBody Device device) {
		deviceService.updateDevice(device);
	}

	/**
	 * 查找所有
	 */
	@GetMapping("/selectAll")
	@ApiOperation(value = "查询所有", notes = "查找所有", position = 4)
	public List<Device> selectAll() {
		return deviceService.findAll();
	}
	/**
	 * 根据id查设备信息
	 */
	@GetMapping("/detail")
	@ApiOperation(value = "查询一个", notes = "", position = 5)
	public R selectById(@ApiParam(value = "deviceId", required = true) @RequestParam String deviceId) {
		return R.data(deviceService.findDeviceById(deviceId));
	}
	/**
	 * 分页查询
	 */
	@GetMapping("/list")
	@ApiOperation(value = "分页", notes = "传入页数和每页显示的条数", position = 6)
	public R<Map<String,Object>> pageList(@ApiParam(value = "页数和条数", required = true)
								  @RequestParam("current") Integer current, @RequestParam("size") Integer size){
		return R.data(deviceService.findAll(current,size));
	}
	/**
	 * 传deviceId
	 */
	@PostMapping("/addSlaveDevice")
	public void addSlaveDevice(@ApiParam(value = "传deviceId", required = true)
								   @RequestParam String slaveId , String deviceId ){
		deviceService.addSlaveDevice(slaveId,deviceId);

	}
	/**
	 * 传deviceId
	 */
	@PostMapping("/removeSlaveDevice")
	public R removeSlaveDevice(@ApiParam(value = "传deviceId", required = true)
							   @RequestParam("slaveId") String slaveId){
		return deviceService.removeSlaveDevice(slaveId)>0?R.success("操作成功"):R.fail("操作失败");
	}
//	/**
//	 * 分页查询
//	 */
//	@GetMapping("/listSlaveDevices")
//	@ApiOperation(value = "分页", notes = "传入页数和每页显示的条数", position = 6)
//	public R<List<Device>> pageListSlaveDevices(@ApiParam(value = "页数和条数", required = true)
//									@RequestParam Integer current, Integer size, String deviceId){
//		return R.data(deviceService.listSlaveDevices(current,size,deviceId));
//	}

	/**
	 * 分页查询
	 */
	@GetMapping("/listSlaveDevices")
	@ApiOperation(value = "分页", notes = "传入页数和每页显示的条数", position = 6)
	public R<Map<String,Object>> listSlaveDevices_nopage(@ApiParam(value = "设备ID", required = true)
												@RequestParam("deviceId") String deviceId,@RequestParam("currentPage")Integer currentPage, @RequestParam("size")Integer size){
		Map<String,Object> listDevices = deviceService.listSlaveDevices(deviceId,currentPage,size);
		return R.data(listDevices);
	}
	/**
	 * 分页查询
	 */
	@GetMapping("/listDeviceShadow")
	@ApiOperation(value = "分页", notes = "传入页数和每页显示的条数", position = 6)
	public R<List<Shadow>> listDeviceShadow_nopage(@ApiParam(value = "设备ID", required = true)
													   @RequestParam String deviceId){
		//return R.data(deviceService.listSlaveDevices(current,size,deviceId));
		List<Shadow> listShadows = deviceService.listDeviceShadow(deviceId);
		return R.data(listShadows);
	}
	/**
	 * 可变参数查询
	 */
	@GetMapping("/selectByCondition")
	@ApiOperation(value = "根据条件查",notes = "可变条件",position = 7)
	public R<Map<String,Object>> selectByCondition(@Valid @RequestParam("deviceName") String deviceName, @RequestParam("productId") String productId, @RequestParam("beginTime") Timestamp beginTime, @RequestParam("endTime")Timestamp endTime,
                                             @RequestParam("isActive") Integer isActive,@RequestParam("current")Integer current,@RequestParam("size")Integer size){
		return R.data(deviceService.selectByCondition(deviceName, productId, beginTime, endTime,
				isActive,current,size));
	}

	/**
	 *说明：根据设备ID查询Topic
	 *@author lilongfei
	 *@since 2019/9/19 14:42
	 */
	@GetMapping("/getTopicListByDeviceId")
	@ApiOperation(value = "根据设备ID查询Topic",notes = "topic列表",position = 8)
	public R getTopicListByDeviceId(@RequestParam("deviceId")String deviceId,@RequestParam("currentPage")Integer currentPage,@RequestParam("size")Integer size){
		return R.data(deviceService.getTopicListByDeviceId(deviceId,currentPage,size));
	}


	/**
	 *说明：获取产品列表
	 *@author lilongfei
	 *@since 2019/9/25 15:11
	 */
	@GetMapping("/getProductList")
	@ApiOperation(value = "查询产品列表",notes = "产品列表",position = 9)
	public R<List<Product>> getProductListByHome(){
		return R.data(this.getProductList());
	}

	/**
	 *说明：查询可关联的设备列表
	 *@author lilongfei
	 *@since 2019/9/26 14:43
	 */
	@GetMapping("/getRelevanceDeviceList")
	@ApiOperation(value = "查询可关联的设备列表",notes = "设备列表",position = 10)
	public R<Map<String,Object>> getRelevanceDeviceList(@RequestParam("page")Integer page,@RequestParam("size")Integer size,@RequestParam("deviceId")String deviceId){
		return R.data(deviceService.getRelevanceDeviceList(page, size,deviceId));
	}

	/**
	 *说明：关联设备
	 *@author lilongfei
	 *@since 2019/9/26 15:21
	 */
	@PostMapping("/connectDevice")
	@ApiOperation(value = "关联设备",notes = "关联设备",position = 11)
	public R connectDevice(@RequestParam("deviceId")String deviceId,@RequestParam("slaveId")String slaveId){
		return deviceService.connectDevice(deviceId, slaveId)>0?R.success("操作成功"):R.fail("操作失败");
	}

	/**
	 *说明：设备发布
	 *@author lilongfei
	 *@since 2019/9/29 14:16
	 */
	@PostMapping("/devicePublish")
	@ApiOperation(value = "设备发布",notes = "发布状态",position = 13)
	public R devicePublish(@RequestParam("deviceId")String deviceId,@RequestParam("publishState")String publishState){
		System.out.println(deviceId+"/"+publishState);
		return deviceService.devicePublish(deviceId,publishState);
	}

}
